import { getFm, fmTrash } from '@/api/author'
import Vue from "vue";
const state = {
  playlist: [{     // 播放列表
    al: {
      id: 1515088,
      name: "Let Go",
      pic: 109951167596882130,
      picUrl: "https://p2.music.126.net/rNWoK0vpHAW1H9kTFKRVig==/109951167596882130.jpg",
      pic_str: "109951167596882130"
    },
    name: 'Let Go',
    ar: [{ name: "Avril Lavigne" }],
    id: 16431886
  }],
  playlistIndex: 0,//默认下标
  isBtnShow: true,//暂停按钮
  detailShow: false,//歌曲详情是否显示
  currentTime: 0,//当前时间
  duration: 0,//歌曲总时长
  musicId: 0,//歌曲id
  /* 播放类型 */
  playType: 'music',//music,personalFm
  Fm: [],//私人fm
}
const actions = {
  // 私人fm
  async personalFm ({ commit, state, dispatch }, operate) {
    if (operate.type == 'get') {
      let res = await getFm()
      if (res.code !== 200) return Vue.prototype.$message.error('获取私人FM失败')
      const list = []
      res.data.forEach(item => {
        list.push({
          id: item.id,
          name: item.name,
          fee: item.fee,
          alia: item.alias,
          ar: item.artists,
          al: item.album,
          dt: item.duration,
          mv: item.mvid
        })
      })
      commit('updatedPlayList', list)
      commit('setPersonalFm', { type: 'set', data: list })
    } else if (operate.type == 'next') {
      if (state.playlistIndex === state.playlist.length - 1) {
        dispatch('personalFm', { type: 'get' })
      } else {
        commit('setPersonalFm', { type: 'next' })
      }
    } else if (operate.type === 'remove') {
      const res = await fmTrash(operate.id)
      if (res.code !== 200) return Vue.prototype.$message.error('操作失败')
      Vue.prototype.$message.success('成功移到垃圾桶')
      dispatch('personalFm', { type: 'next' })
    }

  }
}
const mutations = {
  updatedisBtnShow: function (state, value) {
    state.isBtnShow = value
  },
  updatedPlayList: function (state, value) {
    state.playlist = value
  },
  updatedPlaylistIndex: function (state, value) {
    state.playlistIndex = value
  },
  updateddetailShow: function (state, value) {
    state.detailShow = value
  },
  updatedCurrentTime: function (state, value) {
    state.currentTime = value
  },
  updatedDuration: function (state, value) {
    state.duration = value
  },
  upMusicId (state, value) {
    state.musicId = value
  },
  // 私人fm
  setPersonalFm (state, payload) {
    if (payload.type === 'set') {
      state.playlist = payload.data
      state.playlistIndex = 0
      state.musicId = payload.data[0].id
      state.playType = 'personalFm'
      console.log(state.playlist, payload.data[0].id, state.musicId, 'fffmmm')
    } else if (payload.type === 'next') {
      state.playlistIndex++
      state.musicId = state.playlist[state.playlistIndex].id
    }
  }
}
export default {
  state,
  actions,
  mutations
}